<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="线路标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入线路标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="路线标签" prop="villageLabel">
        <el-select
          v-model="queryParams.villageLabel"
          placeholder="请选择路线标签"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in dict.type.village_label"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="游玩天数" prop="dayNumber">
        <el-input
          v-model="queryParams.dayNumber"
          placeholder="请输入游玩天数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="旅行时间" prop="intervalTime">
        <el-date-picker
          type="daterange"
          value-format="yyyy-MM-dd"
          v-model="queryParams.intervalTime"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['route:travelRoute:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['route:travelRoute:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['route:travelRoute:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['route:travelRoute:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="travelRouteList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="路线表主键"
        align="center"
        prop="id"
        v-if="false"
      />
      <el-table-column label="简介" prop="content" type="expand">
        <template slot-scope="scope">
          <el-form label-position="left" inline class="demo-table-expand">
            <el-form-item label="乡村介绍">
              <div v-html="scope.row.content"></div>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="线路标题" align="center" prop="title" />
      <el-table-column label="路线图片" align="center" prop="image" width="150">
        <template slot-scope="scope">
          <span v-for="(item, index) in scope.row.image" :key="index">
            <el-image
              fit="fit"
              :src="baseUrl + item"
              :preview-src-list="[baseUrl + item]"
              style="width: 50px; height: 50px"
            />
          </span>
        </template>
      </el-table-column>
      <el-table-column label="起始位置" align="center" prop="startPosition" />
      <el-table-column label="结束位置" align="center" prop="endPosition" />
      <el-table-column label="路线标签" align="center" prop="villageLabel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.village_label" :value="scope.row.villageLabel" />
        </template>
      </el-table-column>
      <el-table-column label="游玩天数(天)" align="center" prop="dayNumber" />
      <el-table-column
        label="旅行开始时间"
        align="center"
        prop="startTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="旅行结束时间"
        align="center"
        prop="endTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否推荐" align="center" prop="flag" width="180">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isRecommend" active-color="#13ce66" inactive-color="#ff4949" active-text="是"
            inactive-text="否" @change="flagTableChange(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['route:travelRoute:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['route:travelRoute:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listTravelRoute,
  getTravelRoute,
  delTravelRoute,
  addTravelRoute,
  updateTravelRoute,
  exportTravelRoute,
  updateTravelRecommend,
  townShipList,
  townNameList
} from "@/api/route/travelRoute";
import {
  listRouteTopic,
  getRouteTopic,
  delRouteTopic,
  addRouteTopic,
  updateRouteTopic,
  exportRouteTopic,
} from "@/api/route/routeTopic";


export default {
  name: "TravelRoute",
  dicts: ["travel_state", "village_label"],
  data() {
    return {
      //图片地址
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 旅游路线表格数据
      travelRouteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 旅行状态字典
      travelStateOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        content: null,
        longitude: null,
        latitude: null,
        position: null,
        villageLabel: null,
        dayNumber: null,
        startTime: null,
        endTime: null,
        collectState: null,
        travelState: null,
        image: null,
        intervalTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //乡村标签
      villageGradeOptions: [],
      //起始位置经纬度
      latLng: undefined,
      //乡村列表
      townShipList: [],
      //地址类型
      addressType: "起始位置",
      //乡村列表
      townShipList: [],
    };
  },
  created() {
    this.getList();
    this.getTwonShipList();
    // this.getDicts("travel_state").then(response => {
    //     this.travelStateOptions = response.data;
    // });
    // this.getDicts("village_label").then(response => {
    //     this.villageGradeOptions = response.data;
    // });
  },
  mounted(){
    
  },
  methods: {
    //table表单里是否上架switch开关change事件
    flagTableChange(row) {
      let {isRecommend,id} = row
      console.log('是否推荐',isRecommend,id);
      updateTravelRecommend({id,isRecommend})
      },
    /** 查询旅游路线列表 */
    getList() {
      this.loading = true;
      listTravelRoute(this.queryParams).then((response) => {
        this.travelRouteList = response.rows.map((item) => ({
          ...item,
          image: item.image ? item.image.split(",") : [],
        }));
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询乡村数据列表
    getTwonShipList() {
      townShipList().then((res) => {
        this.townShipList = res.data;
      });
    },
    // 旅行状态字典翻译
    travelStateFormat(row, column) {
      return this.selectDictLabel(this.travelStateOptions, row.travelState);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        title: null,
        content: null,
        longitude: null,
        latitude: null,
        position: null,
        villageLabel: null,
        dayNumber: null,
        startTime: null,
        endTime: null,
        collectState: null,
        travelState: null,
        image: null,
        villageIds: undefined,
        startPosition: null,
        startLatitude: null,
        startLongitude: null,
        endPosition: null,
        endLongitude: null,
        endLatitude: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    
    /** 新增按钮操作 */
    async handleAdd() {
      this.reset();
      let resultTown = await townNameList({ pageSize: 500 });
      // console.log('result',result);
      let resultRoute = await listRouteTopic({pageSize:100})
      // console.log('resultRoute.rows',resultRoute.rows);

      //   this.open = true;
      this.$line.open({
        townShipList:resultTown.rows,
        routeTopicList:resultRoute.rows,
      }, () => {
        this.getList();
      });
    },
    //路线图片上传图片组件成功回调函数
    uploadImg(e) {
      this.form.image = e;
    },
    //起始位置地图组件点击事件
    selectAddressClick(res) {
      if (this.addressType === "起始位置") {
        this.form.startPosition = res.address;
        this.form.startLongitude = res.lnglat.slice(0, res.lnglat.indexOf(","));
        this.form.startLatitude = res.lnglat.slice(
          res.lnglat.indexOf(",") + 1,
          res.lnglat.length
        );
      } else {
        this.form.endPosition = res.address;
        this.form.endLongitude = res.lnglat.slice(0, res.lnglat.indexOf(","));
        this.form.endLatitude = res.lnglat.slice(
          res.lnglat.indexOf(",") + 1,
          res.lnglat.length
        );
      }
    },
    /** 修改按钮操作 */
    async handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      let resultRoute = await listRouteTopic({pageSize:100})
      this.$line.open(
        {
          lineId: id,
          routeTopicList:resultRoute.rows,
        },
        () => {
          this.getList();
        }
      );
      // getTravelRoute(id).then(response => {
      //     this.form = response.data;
      //     this.open = true;
      //     this.title = "修改旅游路线";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTravelRoute(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTravelRoute(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除旅游路线编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delTravelRoute(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有旅游路线数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportTravelRoute(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
